﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeGenLib
{
    public class ParamCreater
    {
        public String CreateInclude(String baseName)
        {
            int index = baseName.IndexOf("::");
            if (index == -1)
            {
                return "\"" + baseName + ".h\"";
            }

            String includeFilename = baseName.Replace("::", "/");
            return "<" + includeFilename + ".h>";
        }

        public String CreateHeaderGuard(String className, String ns)
        {
            return ns + "_" + className + "_h";
            String headerGuard = ns;

            int lastIndex = 0;
            int outIndex = 0;
            while (outIndex != -1)
            {
                outIndex = this.FindSplitIndex(className, lastIndex);
                headerGuard += "_";
                if (outIndex == -1)
                {
                    headerGuard += className.Substring(lastIndex);
                }
                else
                {
                    headerGuard += className.Substring(lastIndex, outIndex - lastIndex);
                }
                lastIndex = outIndex + 1;
            }

            headerGuard += "_h";
            return headerGuard;
        }

        private int FindSplitIndex(String className, int lastIndex)
        {
            int outIndex = -1;
            int upperCount = 0;
            bool found = false;

            for (int index = lastIndex; index < className.Length; ++index)
            {
                if ('A' <= className[index] && className[index] <= 'Z')
                {
                    outIndex = index;
                    ++upperCount;
                    continue;
                }

                if (!found)
                {
                    continue;
                }

                if (upperCount == 1)
                {
                    return outIndex;
                }
                else
                {
                    return outIndex - 1;
                }
            }

            return outIndex;
        }

        public void CreateFilename(GenParam param)
        {
            param.HFilename = param.Class;
            param.CppFilename = param.Class;
        }
    }
}
